package com.lxmes.base.controller;

import com.lxmes.common.annotation.Log;
import com.lxmes.common.core.controller.BaseController;
import com.lxmes.common.core.domain.AjaxResult;
import com.lxmes.common.core.page.TableDataInfo;
import com.lxmes.common.enums.BusinessType;
import com.lxmes.common.utils.poi.ExcelUtil;
import com.lxmes.base.domain.NcSupplier;
import com.lxmes.base.service.INcSupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 供应商管理Controller
 *
 * @author ruoyi
 * @date 2022-11-29
 */
@RestController
@RequestMapping("/supplychain/supplier")
public class NcSupplierController extends BaseController
{
    @Autowired
    private INcSupplierService ncSupplierService;

    /**
     * 查询供应商管理列表
     */
    @PreAuthorize("@ss.hasPermi('supplychain:supplier:list')")
    @GetMapping("/list")
    public TableDataInfo list(NcSupplier ncSupplier)
    {
        startPage();
        List<NcSupplier> list = ncSupplierService.selectNcSupplierList(ncSupplier);
        return getDataTable(list);
    }
    @GetMapping(value = "/getLiSupplier")
    public AjaxResult getLiSupplier()
    {
        List<NcSupplier> list = ncSupplierService.selectNcSupplierList(new NcSupplier());
        return AjaxResult.success(list);
    }

    /**
     * 导出供应商管理列表
     */
    @PreAuthorize("@ss.hasPermi('supplychain:supplier:export')")
    @Log(title = "供应商管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NcSupplier ncSupplier)
    {
        List<NcSupplier> list = ncSupplierService.selectNcSupplierList(ncSupplier);
        ExcelUtil<NcSupplier> util = new ExcelUtil<NcSupplier>(NcSupplier.class);
        util.exportExcel(response, list, "供应商管理数据");
    }

    /**
     * 获取供应商管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('supplychain:supplier:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ncSupplierService.selectNcSupplierById(id));
    }

    /**
     * 新增供应商管理
     */
    @PreAuthorize("@ss.hasPermi('supplychain:supplier:add')")
    @Log(title = "供应商管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NcSupplier ncSupplier)
    {
        return toAjax(ncSupplierService.insertNcSupplier(ncSupplier));
    }

    /**
     * 修改供应商管理
     */
    @PreAuthorize("@ss.hasPermi('supplychain:supplier:edit')")
    @Log(title = "供应商管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NcSupplier ncSupplier)
    {
        return toAjax(ncSupplierService.updateNcSupplier(ncSupplier));
    }
    /**
     * 导入供应商档案/份额调整/份额控制总
     */
    @Log(title = "供应商档案", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<NcSupplier> util = new ExcelUtil<>(NcSupplier.class);
        List<NcSupplier> ncSupplierList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = ncSupplierService.imporIteminfo(ncSupplierList, updateSupport, operName);
        return AjaxResult.success(message);
    }
    // 下载模板
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<NcSupplier> util = new ExcelUtil<>(NcSupplier.class);
        util.importTemplateExcel(response,"供应商档案");
    }
    /**
     * 删除供应商管理
     */
    @PreAuthorize("@ss.hasPermi('supplychain:supplier:remove')")
    @Log(title = "供应商管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ncSupplierService.deleteNcSupplierByIds(ids));
    }
}
